﻿/**
 * Tophy's Pagecontrol class
 * to control page navigation 
 * 
 */
dynamic class PageControl {
	public static var M_NAVIGATE:String = "onNavigateFunction"; //function (page:Number)
		private var __selectedTextFormat:TextFormat;
		private var __notSelectedTextFormat:TextFormat ;
		private var _pageTxt:TextField;
		private var _nowPage:Number=0;
		private var _totalPage:Number = 0;
		private var _recordProPage:Number = 10;
		private var _totalRecordCount:Number = 0;
		private var _showPageNum:Number = 5;
		private var _navigeFunName:String;
		
		
		public function get SelectedTextFormat():TextFormat {
			return this.__selectedTextFormat;
		}
		public function set SelectedTextFormat(tf:TextFormat) {
			this.__selectedTextFormat = tf;
			this.buildTxt();
		}
		
		public function get UnSelectedTextFormat():TextFormat {
			return this.__notSelectedTextFormat;
		}
		public function set UnSelectedTextFormat(tf:TextFormat) {
			this.__notSelectedTextFormat = tf;
			this.buildTxt();
		}
		
		public function get PageTxt():TextField {
			return this._pageTxt;
		}
		public function set PageTxt(txt:TextField) {
			this._pageTxt = txt;
			this.buildTxt();
		}
		public function get NowPage():Number {
			return _nowPage;
		}
		public function set NowPage(value:Number){
			_nowPage = value;
			seekPage(value);
		}
		public function get ShowPageNum():Number {
			return this._showPageNum;
		}
		public function set ShowPageNum(value:Number) {
			this._showPageNum = value;
			buildTxt();
		}
		public function get TotalPage():Number {
			return this._totalPage;
		}
		public function set TotalPage(count:Number) {
			if (this._totalPage != count) {
				INIT(this._pageTxt, this._nowPage, this._showPageNum, count);
				this._totalPage = count;
			}
		}
		public function get TotalRecordCount():Number {
			return this.TotalRecordCount;
		}
		public function set TotalRecordCount(count:Number) {
			if (this._totalRecordCount != count) {
				INIT(this._pageTxt, this._nowPage, this._showPageNum, null, count, this._recordProPage);
				this._totalRecordCount = count;
			}
		}
		public function get RecordPrePage():Number {
			return this._recordProPage;
		}
		public function set RecordPrePage(count:Number) {
			if (this._recordProPage != count) {
				INIT(this._pageTxt, this._nowPage, this._showPageNum, null, this._totalRecordCount, count);
				this._totalRecordCount = count;
			}
		}
		public function get NavigateFunName():String {
			return this._navigeFunName;
		}
		public function set NavigateFunName(name:String) {
			this._navigeFunName = name;
			this.buildTxt();
		}
		
		
		
	private var pages:Array;
	private var nowshowedpage:Array;
	
	public function PageControl(txt:TextField) {
		_navigeFunName = "";
		this._pageTxt = txt;
		__selectedTextFormat = new TextFormat("arial", 10, 0, true);
		__notSelectedTextFormat = new TextFormat("airal", 9, 0xcccccc, false)
		AsBroadcaster.initialize(this);
		trace("tophy's pagecontrol initd!!");
	}
	public function INIT(txt:TextField, nowpage:Number, showpagenum:Number, totalpage:Number, totalrecord:Number, recordPrePage:Number) {
		if (txt == undefined) txt = this._pageTxt;
		if (nowpage == undefined) nowpage = this._nowPage;
		if (showpagenum == undefined) showpagenum = this._showPageNum;
		if (totalpage == undefined && (totalrecord == undefined || recordPrePage == undefined)) {
			totalpage = this._totalPage;
			totalrecord = this._totalRecordCount;
			recordPrePage = this._recordProPage;
		}else if (totalpage != undefined && (totalrecord == undefined || recordPrePage == undefined)) {
			this._totalPage = totalpage;
		}else if(totalpage == undefined && (totalrecord != undefined && recordPrePage != undefined)) {
			this._recordProPage = recordPrePage;
			this._totalRecordCount = totalrecord;
			this._totalPage = Math.ceil(this._totalRecordCount / this._recordProPage);
		}
		if (txt == undefined) {
			trace("select-page-txt not set!");
			return;
		}else {
			this._pageTxt = txt;
		}
		if (showpagenum == undefined || showpagenum<0) {
			trace("number of pageindex to show is undefine");
			return;
		}else {
			_showPageNum = showpagenum;
		}
		
		pages = new Array();
		for (var i = 1 ; i <= this._totalPage; i++) {
			pages.push(i);
		}
		//trace(pages)
		seekPage(nowpage);
	}
	
	public function seekPage(newNowPage:Number) {
		if (newNowPage == undefined || newNowPage < 1) {
			newNowPage = 1;
		}
		if (newNowPage > this._totalPage) {
			newNowPage = this._totalPage;
		}
		this.nowshowedpage = new Array();
		this.nowshowedpage.push(newNowPage);
		fullPageToArray(this.nowshowedpage, this._showPageNum - 1, (newNowPage > this._nowPage));
		this._nowPage = newNowPage;
		buildTxt();
		this.broadcastMessage(M_NAVIGATE, this._nowPage);
	}
	public function NextPage() {
		if (this._nowPage <= 0) return;
		var nextpage:Number = this._nowPage + 1;
		seekPage(nextpage);
	}
	public function ProPage() {
		if (this._nowPage <= 0) return;
		var nextpage:Number = this._nowPage - 1;
		seekPage(nextpage);
	}
	public function ToHead() {
		if (this._nowPage <= 0) return;
		seekPage(1);
	}
	public function ToEnd() {
		if (this._nowPage <= 0) return;
		seekPage(this.pages[this.pages.length - 1]);
	}
	private function buildTxt():String {
		if (this.nowshowedpage == undefined || this.nowshowedpage.length <= 0) return;
		this._pageTxt.html = true;
		this._pageTxt.htmlText = "";
		//trace(this._pageTxt)
		//return;
		var sbp:String = "";
		var sep:String = "";
		var usbp:String = "";
		var usep:String = "";
		
		if (__selectedTextFormat.bold) {
			sbp += "<b>";
			sep = "</b>" + sep;
		}
		if (__selectedTextFormat.italic) {
			sbp += "<i>";
			sep = "</i>" + sep;
		}
		if (__notSelectedTextFormat.bold) {
			usbp += "<b>";
			usep = "</b>" + sep;
		}
		if (__notSelectedTextFormat.italic) {
			usbp += "<i>";
			usep = "</i>" + sep;
		}
		
		sep += "</font>";
		usep += "</font>";
		
		sbp = "<font face='" + this.__selectedTextFormat.font + "' size='" + this.__selectedTextFormat.size + "' color='#" + this.__selectedTextFormat.color.toString(16) + "'>" + sbp;
		usbp = "<font face='" + this.__notSelectedTextFormat.font + "' size='" + this.__notSelectedTextFormat.size + "' color='#" + this.__notSelectedTextFormat.color.toString(16) + "'>" + usbp;
		
		var functionstr:String = "";
		var efunctionstr:String = "";
		var fu:Boolean = false;
		if (this._navigeFunName != "") {
			functionstr = "<a href='asfunction:" + this._navigeFunName + ",";
			efunctionstr = "</a>";
			fu = true;
		}
		
		var htmltxt:String = "";
		for (var i = 0; i < this.nowshowedpage.length; i++) {
			if(this._nowPage==this.nowshowedpage[i]){
				htmltxt += functionstr+((fu)? this.nowshowedpage[i]+"'>":"") +sbp + this.nowshowedpage[i] + " " + sep + efunctionstr;
			}else {
				htmltxt += functionstr+((fu)? this.nowshowedpage[i]+"'>":"") +usbp + this.nowshowedpage[i] + " " + usep + efunctionstr;
			}
		}
	//	<font size='9' color='#333333' > ><b>6</font><font size='9' color='#0' ><b>7</b></font><font size='9' color='#333333' > ><b>8</font><font size='9' color='#333333' > ><b>9</font>
		this._pageTxt.htmlText = htmltxt;
		return htmltxt;
	}
	private function fullPageToArray(fullArr:Array, count:Number, left:Boolean) {
		
		if (fullArr.length == 0) return;
		if (count <= 0) return;
		
		var lefti:Number = fullArr[0];
		var righti:Number = fullArr[fullArr.length - 1];
		if (lefti == 1 && righti == this.pages.length) return;
		if (left) {
			if (lefti == 1) {
				fullPageToArray(fullArr, count, false);
			}
			else {
				fullArr.unshift(this.pages[lefti - 2]);
				fullPageToArray(fullArr, --count, false);
			}
		}else {
			if (righti == this.pages.length) {
				fullPageToArray(fullArr, count, true);
			}else {
				fullArr.push(this.pages[righti]);
				fullPageToArray(fullArr, --count, true);
			}
		}
	}
}
















